package com.fnzx.util.oss;

import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author hst
 * @create 2021-10-12 16:54
 * @Description:
 */
public class OssUtils {

    // Endpoint以杭州为例，其它Region请按实际情况填写。
    public static String endpoint = ConstantOssPropertiesUtils.EDNPOINT;
    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录RAM控制台创建RAM账号。
    public static String accessKeyId = ConstantOssPropertiesUtils.ACCESS_KEY_ID;
    public static String accessKeySecret = ConstantOssPropertiesUtils.SECRECT;
    public static String bucketName = ConstantOssPropertiesUtils.BUCKET;
    // 创建OSSClient实例。
    public static OSS ossClient;

    public static String upload(MultipartFile file) {

        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            //上传文件流
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            //1 为了避免相同的文件名的文件进行覆盖，所以生成随机唯一值，使用uuid，添加到文件名称来
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            //uuid只能放在前面
            fileName  = uuid + fileName;
            //2 按照当前日期，创建文件夹，上传到创建文件夹里面 /2021/02/02/02.jpg
            String timeUrl = new DateTime().toString("yyyy/MM/dd");
            fileName = timeUrl + "/" + fileName;

            //调用方法实现上传
            ossClient.putObject(bucketName,fileName,inputStream);
            //上传之后的文件的路径
            //https://yygh-hst.oss-cn-beijing.aliyuncs.com/12314345435.jpg
            String url = "https://" + bucketName + "." + endpoint + "/" + fileName;
            return url;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            ossClient.shutdown();
        }
    }

    public static void delete(String url) {
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            String head = "https://power-center.oss-cn-beijing.aliyuncs.com/";
            url = url.substring(head.length());
            ossClient.deleteObject(bucketName,url);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ossClient.shutdown();
        }
    }


    public static void down(String url,String localPath,String saveName){

        File file1 = new File(localPath);
        try{
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            String head = "https://power-center.oss-cn-beijing.aliyuncs.com/";
            url = url.substring(head.length());
            ossClient.getObject(new GetObjectRequest(bucketName,url),new File(localPath+saveName));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            ossClient.shutdown();
        }
    }

}
